package com.table;

import com.appclient.ClientWizardController;
import com.appclient.DaoInstanceEnum;
import com.carpark.ejb.Model.CarsModel;
import com.carpark.ejb.Model.ParkingpassesModel;
import com.carpark.ejb.Model.TarrifsModel;
import com.wizardOther.Items.ValidationForm;
import java.util.ArrayList;
import java.util.List;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;

/**
 *
 * @author Vi
 */
public class ParkingPassesController extends ValidationForm {

    @FXML
    private TextField filterField;
    @FXML
    private TableView<ParkingpassesModel> passesTable;
    @FXML
    private TableColumn<ParkingpassesModel, Integer> idNumber;
    @FXML
    private TableColumn<ParkingpassesModel, Integer> carId;
    @FXML
    private TableColumn<ParkingpassesModel, Integer> spaceNumber;
    @FXML
    private TableColumn<ParkingpassesModel, String> start;
    @FXML
    private TableColumn<ParkingpassesModel, String> end;
        @FXML
    private TableColumn<ParkingpassesModel, Integer> tarrif;
    @FXML
    private Button choose;
    private ObservableList<ParkingpassesModel> masterData = FXCollections.observableArrayList();
    private ObservableList<ParkingpassesModel> filteredData = FXCollections.observableArrayList();
    private Stage dialogStage;
    private Integer selectedPass;

    public Integer getSelectedPass() {
        return selectedPass;
    }

    public ParkingPassesController() {
        List<ParkingpassesModel> data = DaoInstanceEnum.INSTANCE.getDao().getAllParkingPasses();
        for (ParkingpassesModel pass : data) {

            masterData.add(pass);
        }

        filteredData.addAll(masterData);
        masterData.addListener(new ListChangeListener<ParkingpassesModel>() {
            @Override
            public void onChanged(ListChangeListener.Change<? extends ParkingpassesModel> change) {
                updateFilteredData();
            }
        });
    }

    /**
     * Initializes the controller class. This method is automatically called
     * after the fxml file has been loaded.
     */
    @FXML
    private void initialize() {
        idNumber.setCellValueFactory(
                new PropertyValueFactory<ParkingpassesModel, Integer>("parkingPassId"));
        carId.setCellValueFactory(
                new PropertyValueFactory<ParkingpassesModel, Integer>("carId"));
       spaceNumber.setCellValueFactory(
                new PropertyValueFactory<ParkingpassesModel, Integer>("numberId"));
        start.setCellValueFactory(
                new PropertyValueFactory<ParkingpassesModel, String>("dateIssue"));
        end.setCellValueFactory(
                new PropertyValueFactory<ParkingpassesModel, String>("expirationDate"));
   tarrif.setCellValueFactory(
                new PropertyValueFactory<ParkingpassesModel, Integer>("tarrif"));

        passesTable.setItems(filteredData);

        filterField.textProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> observable,
                    String oldValue, String newValue) {
                updateFilteredData();
            }
        });
    }

    /**
     * Updates the filteredData to contain all data from the masterData that
     * matches the current filter.
     */
    private void updateFilteredData() {
        filteredData.clear();

        for (ParkingpassesModel p : masterData) {
            if (matchesFilter(p)) {
                filteredData.add(p);
            }
        }

        // Must re-sort table after items changed
        reapplyTableSortOrder();
    }

    /**
     * Returns true if the person matches the current filter. Lower/Upper case
     * is ignored.
     *
     * @param pass
     * @return
     */
    private boolean matchesFilter(ParkingpassesModel pass) {
        String filterString = filterField.getText();
        if (filterString == null || filterString.isEmpty()) {
            return true;
        }
        String lowerCaseFilterString = filterString.toLowerCase();

        if (pass.getDateIssue().toString().toLowerCase().indexOf(lowerCaseFilterString) != -1) {
            return true;
        }
        if (pass.getExpirationDate().toString().toLowerCase().indexOf(lowerCaseFilterString) != -1) {
            return true;
        }
        if (pass.getTarrif().getName().toLowerCase().indexOf(lowerCaseFilterString) != -1) {
            return true;
        }
        if (pass.getCarId().getMake().toLowerCase().indexOf(lowerCaseFilterString) != -1) {
            return true;
        }
        if (pass.getCarId().getModel().toLowerCase().indexOf(lowerCaseFilterString) != -1) {
            return true;
        }
        if (pass.getCarId().getNumberPlate().toLowerCase().indexOf(lowerCaseFilterString) != -1) {
            return true;
        }

        return false;
    }

    public void setDialogStage(Stage dialogStage) {
        this.dialogStage = dialogStage;
    }

    private void reapplyTableSortOrder() {
        ArrayList<TableColumn<ParkingpassesModel, ?>> sortOrder = new ArrayList<>(passesTable.getSortOrder());
        passesTable.getSortOrder().clear();
        passesTable.getSortOrder().addAll(sortOrder);
    }

    @FXML
    private void makeChoice(ActionEvent event) {
        if (event.getSource() == choose) {
            try {
                selectedPass = passesTable.getSelectionModel().getSelectedItem().getParkingPassId();
            } catch (Exception e) {
                createPopup("Zaznacz najpierw w tabeli pojazd!");
            }
            if (selectedPass != null) {
                dialogStage.close();
            }
        }
    }

}
